package edu.guigu.appquickstart.recyclerview.ex2;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smart.refresh.footer.BallPulseFooter;
import com.scwang.smart.refresh.header.BezierRadarHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

import edu.guigu.appquickstart.R;
import edu.guigu.appquickstart.base.BaseActivity;
import edu.guigu.appquickstart.recyclerview.bean.PersonBean;
import edu.guigu.appquickstart.recyclerview.ex1.ShowActivity;

public class PersonListActivity extends BaseActivity implements View.OnClickListener {

    private RecyclerView recyclerView;
    private ArrayList<PersonBean> datas;
    private PersonBeanAdapter adapter;

    private View rv_normal_show;
    private View lay_bottom;
    //    private SwipeRefreshLayout swiperefreshlayout;
    private SmartRefreshLayout smartRefreshLayout;
    private TextView tv_check_all, tv_delete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_list);
        // Actionbar
        setSupportActionBar(findViewById(R.id.toolbar));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        // 初始化数据
        initData();
        // 初始化视图
        initView();
        // 初始化监听
        initListener();
    }


    private void initData() {
        datas = new ArrayList<>();
        datas.addAll(PersonBean.loadData(PersonListActivity.this));
    }

    private void initView() {
        // ----------------------------------------
        recyclerView = findViewById(R.id.recyclerview);
        smartRefreshLayout = findViewById(R.id.swiperefreshlayout);
        lay_bottom = findViewById(R.id.lay_bottom);
        rv_normal_show = findViewById(R.id.rv_normal_show);
        tv_check_all = findViewById(R.id.tv_check_all);
        tv_delete = findViewById(R.id.tv_delete);
        // recyclerView隐
        rv_normal_show.setVisibility(View.VISIBLE);
        recyclerView.setVisibility(View.GONE);
        lay_bottom.setVisibility(View.GONE);
        // ----------------------------------------
        adapter = new PersonBeanAdapter(this, datas);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        // ----------------------------------
        smartRefreshLayout.setRefreshHeader(new BezierRadarHeader(this));
        smartRefreshLayout.setRefreshFooter(new BallPulseFooter(this));
    }

    private void initListener() {
        tv_check_all.setOnClickListener(this);
        tv_delete.setOnClickListener(this);
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                datas.clear();
                datas.addAll(PersonBean.loadData(PersonListActivity.this));
                adapter.notifyDataSetChanged();
                // recyclerView显
                recyclerView.setVisibility(View.VISIBLE);
                rv_normal_show.setVisibility(View.GONE);
                refreshlayout.finishRefresh(1000/*,false*/);//传入false表示刷新失败
            }
        });
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(1000/*,false*/);//传入false表示加载失败
            }
        });

        adapter.setOnMyItemViewOnClickListener(new PersonBeanAdapter.OnMyItemViewOnClickListener() {
            @Override
            public void onClick(View view, int position) {
                // 跳转
                if (!PersonBean.isShow) {
                    Intent intent = new Intent(PersonListActivity.this, ShowActivity.class);
                    intent.putExtra("html_url", datas.get(position).getUrl());
                    startActivity(intent);
                    return;
                }
                // 进入编辑模式
                final PersonBean currentPerson = datas.get(position);
                currentPerson.setChecked(!currentPerson.isChecked());
                adapter.notifyItemChanged(position);
            }
        });
    }

    @Override
    public void onClick(View v) {
        // 全选
        if (v.getId() == R.id.tv_check_all) {
            if (!PersonBean.isShow) return;
            if (tv_check_all.getText().equals("全选")) {
                for (PersonBean personBean : datas) {
                    personBean.setChecked(true);
                }
                tv_check_all.setText("全不选");
            } else if (tv_check_all.getText().equals("全不选")) {
                for (PersonBean personBean : datas) {
                    personBean.setChecked(false);
                }
                tv_check_all.setText("全选");
            }
            adapter.notifyDataSetChanged();
        }
        // 删除
        if (v.getId() == R.id.tv_delete) {
            if (!PersonBean.isShow) return;
            tv_check_all.setText("全选");
            // TODO： 注意这里从后向前删除
            for (int i = datas.size() - 1; i >= 0; i--) {
                PersonBean personBean = datas.get(i);
                if (personBean.isChecked()) {
                    datas.remove(i);
                }
            }
            adapter.notifyDataSetChanged();
            // 如果全删完了
            if (datas.size() <= 0) {
                // recyclerView隐
                rv_normal_show.setVisibility(View.VISIBLE);
                recyclerView.setVisibility(View.GONE);
            }
        }
    }

    // 选项菜单----------------------------------------------------------------
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_recyclerview_ex2, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        // 点选项菜单
        if (item.getItemId() == R.id.menu_edit) {
            // 检查底部视图显示情况
            switch (lay_bottom.getVisibility()) {
                // 底部视图不可见时，当前处于未编辑状态
                case View.GONE:
                    // 进入编辑状态，底部视图更新为可见，菜单更新为“取消”
                    lay_bottom.setVisibility(View.VISIBLE);
                    item.setTitle("取消");
                    tv_check_all.setText("全选");
                    PersonBean.isShow = true;
                    break;
                // 底部视图可见时，当前处于编辑状态
                case View.VISIBLE:
                    // 进入未编辑状态，底部视图更新为不可见，菜单更新为“编辑”
                    lay_bottom.setVisibility(View.GONE);
                    item.setTitle("编辑");
                    tv_check_all.setText("全选");
                    PersonBean.isShow = false;
                    for (PersonBean personBean : datas) {
                        personBean.setChecked(false);
                    }
                    break;
            }
            adapter.notifyDataSetChanged();
        }
        return super.onOptionsItemSelected(item);
    }

}